package com.cheer.tool;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.extra.tokenizer.Word;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.word.WordUtil;
import com.cheer.util.ToolUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.poi.hwpf.converter.WordToTextConverter;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.aspectj.util.FileUtil;

import java.io.*;
import java.util.*;


public class VoteUpImport {

    public static void main(String[] args) throws IOException {
        // excel 数据
        // ExcelReader reader = ExcelUtil.getReader("F:\\大学习\\活动点赞\\向上向善\\向上向善汇总\\向上向善汇总表.xlsx");
        ExcelReader reader = ExcelUtil.getReader("F:\\大学习\\活动点赞\\寻访宣传人物\\黑龙江省“乡村振兴 青年先锋”寻访宣传人物名单.xlsx");
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        int sheetCount = reader.getSheetCount();
        int count = 0;

        Set<String> fileType = new HashSet<>();
        for (int i = 0; i <sheetCount; i++) {
            ExcelReader sheetContent = ExcelUtil.getReader("F:\\大学习\\活动点赞\\寻访宣传人物\\黑龙江省“乡村振兴 青年先锋”寻访宣传人物名单.xlsx", i);
            List<Map<String, Object>> readAll = sheetContent.readAll();

            for (int j = 0; j < readAll.size(); j++) {
                // 类别	编号	推荐人	单位
                Map<String, Object> stringObjectMap = readAll.get(j);
                String dirName = String.valueOf(stringObjectMap.get("类别"));
                String number = String.valueOf(stringObjectMap.get("编号"));
                String recommendPeople = String.valueOf(stringObjectMap.get("推荐人"));
                String remark = String.valueOf(stringObjectMap.get("单位"));
                String intro = "";
                String story = "";
                String image = "vote6/";
                String absolutePath = "F:\\大学习\\活动点赞\\寻访宣传人物\\图像汇总" + File.separator;
                // 读取文件下的图片个个人事迹
                String filePath = "F:\\大学习\\活动点赞\\寻访宣传人物" + File.separator + dirName + File.separator + number+"."+recommendPeople.replaceAll("\\s", "") + File.separator;
                String[] strings = FileUtil.listFiles(new File(filePath));
                for (int k = 0; k < strings.length; k++) {
                    if(strings[k].endsWith("个人故事.txt")){

                        String charset = ToolUtils.getCharset(new File(filePath + strings[k]));
                        story = cn.hutool.core.io.FileUtil.readString(new File(filePath+strings[k]), charset);
                    }

                    if(strings[k].equals("个人简介.txt")){
                        String charset = ToolUtils.getCharset(new File(filePath + strings[k]));
                        intro = cn.hutool.core.io.FileUtil.readString(new File(filePath+strings[k]), charset);
                    }
                    if(strings[k].contains("头像")){
                        // 重命名文件到指定文件夹下
                        String photoName = RandomUtil.randomNumbers(15)+"."+ cn.hutool.core.io.FileUtil.extName(strings[k]);
                        // 将当前文件copy
                        FileUtil.copyFile(new File(filePath + strings[k]), new File(absolutePath + photoName));
                        image += photoName;
                    }

                    // word 文本读取
                    if(strings[k].endsWith(".docx") || strings[k].endsWith(".doc")){
                        try {
                            intro = StrUtil.trim(read(filePath+strings[k]));
                            intro = StrUtil.trim(intro.replaceFirst(".*\\s", ""));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    fileType.add(strings[k].split("\\.")[1]);
                    if(strings[k].endsWith(".jpg") || strings[k].endsWith(".bmp") || strings[k].endsWith(".JPG")|| strings[k].endsWith(".png") ){
                        // 重命名文件到指定文件夹下
                        String photoName = md5.digestHex(strings[k]) +"."+ cn.hutool.core.io.FileUtil.extName(strings[k]);
                        // 将当前文件copy
                        FileUtil.copyFile(new File(filePath + strings[k]), new File(absolutePath + photoName));
                        image += photoName;
                    }
                }


                // 打印sql
                String exVotePersonSql = ToolUtils.getExVotePersonSql(1080 + count, "6", getCategoryIdByName(dirName), ToolUtils.getExVotePersonPersonNumber(String.valueOf(count + 1)), recommendPeople, image, remark);
                System.out.println(exVotePersonSql);
                String exVotePersonExtraSql = ToolUtils.getExVotePersonExtraSql(1080 + count, intro, story);
                System.out.println(exVotePersonExtraSql);
                count++;
            }

        }

        System.out.println("文件类型输出：" + fileType);
    }

    static String read(String file) throws Exception {
        InputStream is = new BufferedInputStream(new FileInputStream(file)); //really a OOXML Word file
        String text = "";
        if (FileMagic.valueOf(is) == FileMagic.OLE2) {
            WordExtractor ex = new WordExtractor(is);
            text = ex.getText();
            ex.close();
        } else if (FileMagic.valueOf(is) == FileMagic.OOXML) {
            XWPFDocument doc = new XWPFDocument(is);
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
            text = extractor.getText();
            extractor.close();
        }
        return text;
    }


    /**
     * 获取分类ID
     * @param dirName
     * @return
     */
    private static String getCategoryIdByName(String dirName) {

        Map<String, String> map = new HashMap<>();
        map.put("涉农创业","18");
        map.put("乡村治理","19");
        map.put("科技兴农","20");
        map.put("创新创业","21");
        return map.get(dirName);
    }


}
